package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Donors;
import company.domain.Pets;
import company.domain.Rescuestations;
import company.service.DonorsService;
import company.service.PetsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class PetsController {
    @Resource
    PetsService petsService;

    /*
     * 模糊分页查询
     * */
    @RequestMapping("/selectAllPets/{page}")
    public R selectAllPets(@PathVariable("page") int page, @RequestBody Pets pets){
        QueryWrapper<Pets> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(pets.getName()!="" && !("".equals(pets.getName())),"name",pets.getName());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,3);
        List<Pets> list=this.petsService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
    * 新增宠物
    * */

    @PostMapping("/AddPets")
    public R AddPets(@RequestBody Pets pets){
        this.petsService.save(pets);
        return new R();
    }

    /*
     * 宠物删除
     * */
    @RequestMapping("/DelPets/{id}")
    public R DelPets(@PathVariable("id") int id){
        boolean remove = this.petsService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据宠物id查询
     * */
    @PostMapping("/selectPets")
    public R selectPets(@RequestBody Pets pets){
        Pets byId = this.petsService.getById(pets.getPetid());
        return new R().add("byId",byId);
    }

    /*
     * 修改宠物
     * */
    @PostMapping("/uploadPets")
    public R uploadPets(@RequestBody Pets pets){
        this.petsService.updateById(pets);
        return new R();
    }

    @PostMapping("/uploadfilePets")
    public R uploadFile2(@RequestParam("file2") MultipartFile file2) throws IOException {
        System.out.println(file2.getOriginalFilename());
        file2.transferTo(new File("D:\\HTML\\cat-html\\CAY\\img"+file2.getOriginalFilename()));
        return new R().add("fname2",file2.getOriginalFilename());
    }

    @RequestMapping("/selecaaaa")
    public R selecaaaa(){
        List<Pets> list = this.petsService.list();
        return new R().add("list",list);
    }

    /*
     * 连表查询
     * */
    @RequestMapping("/selectpets/{page}")
    public R selectPets(@PathVariable("page") int page){
        PageHelper.startPage(page,2);
        List<Map> list = petsService.selectAll();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }
}
